package com.yft.entity.base;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

public class Order {
	
	private List<OrderBy> orders = new ArrayList<OrderBy>();
	
	private class OrderBy{
		private String orderName;
		private String orderSort;
		
		private OrderBy(String orderName,String orderSort){
			this.orderName = orderName;
			this.orderSort = orderSort;
		}

		private String getOrderName() {
			return orderName;
		}

		private String getOrderSort() {
			return orderSort;
		}
	}

	public void addOrder(String orderName,OrderSort orderSort){
		orders.add(new OrderBy(orderName,orderSort.getDesc()));
	}
	
	public void addIndexOrder(int index,String orderName,OrderSort orderSort){
		int c = orders.size();
		if(index<0 || (index!=0 && index>c-1)) return;
		orders.add(index, new OrderBy(orderName,orderSort.getDesc()));
	}
	
	public String toString(){
		String orderStr = "";
		for (OrderBy o:orders){
			orderStr += o.getOrderName()+" "+o.getOrderSort()+",";
		}
		
		orderStr = StringUtils.substringBeforeLast(orderStr, ",");
		return orderStr.length()<1? "":" order by "+orderStr;
		
	}
}
